package com.example.lenovo.weiyi;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.com.ruanmeng.demon.MyServiceM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.BaseLoadMoreHeaderAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;


public class MyServiceActivity extends BaseActivity {

    @BindView(R.id.rlist_myservice)
    RecyclerView rlistMyservice;
    @BindView(R.id.refresh_myservice)
    SwipeRefreshLayout mRefresh;
    private LinearLayout li_myservice_null;
    ArrayList<MyServiceM.RowsBean> Temp_list = new ArrayList<>();
    private MyServiceListAdapter mAdapter;
    int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_service);
        ButterKnife.bind(this);
        AddActivity(this);
        ShowLayOrder();
        ChangLayTitle("我的服务");
        LayBack();
        init();
        getData(true);
    }

    private void init() {
        //空
        li_myservice_null = (LinearLayout) findViewById(R.id.li_myservice_null);
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData(true);
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rlistMyservice.setLayoutManager(linearLayoutManager);
        mAdapter = new MyServiceListAdapter(MyServiceActivity.this, rlistMyservice, Temp_list, R.layout.item_myservice);
        rlistMyservice.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent in = new Intent(MyServiceActivity.this, ServiceUseActivity.class);
                in.putExtra("userProductId", Temp_list.get(position + 1).getUserProductId());
                startActivity(in);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData(false);
            }
        });
    }

    /**
     * 获取我的服务列表
     */
    public void getData(Boolean b) {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.MyService, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(MyServiceActivity.this, "uid"));
        mRequest.add("page", page);
        // 添加到请求队列
        CallServer.getRequestInstance().add(MyServiceActivity.this, 0, mRequest,
                new CustomHttpListener(MyServiceActivity.this, true, MyServiceM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        MyServiceM model = ((MyServiceM) data);
                        if (page == 1) {
                            Temp_list.clear();
                        }
                        Temp_list.addAll(model.getRows());
                        mAdapter.notifyDataSetChanged();

                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if (!("100").equals(obj.getString("msgcode"))) {
                            CommonUtil.showToask(MyServiceActivity.this, obj.getString("msg"));
                        }
                        if (Temp_list.size() == 0) {
                            li_myservice_null.setVisibility(View.VISIBLE);
                            rlistMyservice.setVisibility(View.GONE);
                        }
                        mRefresh.setRefreshing(false);
                        mAdapter.setLoading(false);

                    }
                }, true, b);
    }


    public class MyServiceListAdapter extends BaseLoadMoreHeaderAdapter<MyServiceM.RowsBean> {


        public MyServiceListAdapter(Context mContext, RecyclerView recyclerView, List<MyServiceM.RowsBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, RecyclerView.ViewHolder holder, MyServiceM.RowsBean s) {
            TextView name = (TextView) holder.itemView.findViewById(R.id.tv_mservice_name);
            TextView time = (TextView) holder.itemView.findViewById(R.id.tv_mservice_time);
            View li_ban = (View) holder.itemView.findViewById(R.id.view_mservice_ban);
            View li_man = (View) holder.itemView.findViewById(R.id.view_mservice_man);
            name.setText(s.getTitle());
            time.setText(s.getProNum() + "次");
            if (holder.getLayoutPosition() == Temp_list.size() - 1) {
                li_ban.setVisibility(View.GONE);
                li_man.setVisibility(View.VISIBLE);
            } else {
                li_ban.setVisibility(View.VISIBLE);
                li_man.setVisibility(View.GONE);
            }
        }
    }

    public void doClick(View v) {
        switch (v.getId()) {
            case R.id.tv_lay_myorder:
                Intent in = new Intent(MyServiceActivity.this, MyOrderActivity.class);
                startActivity(in);
                break;
        }
    }

}
